package zhouzhouyongche.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zhouzhouyongche.client_util.ClientTicket;
import zhouzhouyongche.dao.CityDao;
import zhouzhouyongche.dao.DayDao;
import zhouzhouyongche.dao.HourDao;
import zhouzhouyongche.dao.MinuteDao;
import zhouzhouyongche.dao.MonthDao;
import zhouzhouyongche.dao.PassengerStationDao;
import zhouzhouyongche.dao.SiteDao;
import zhouzhouyongche.dao.TicketDao;
import zhouzhouyongche.dao.YearDao;
import zhouzhouyongche.model.Ticket;
import zhouzhouyongche.service.TicketService;

@Service
public class TicketServiceImpl implements TicketService{

	@Autowired
	private TicketDao ticketDao;
	@Autowired
	private CityDao cityDao;
	@Autowired
	private SiteDao siteDao;
	@Autowired
	private PassengerStationDao passengerStationDao;
	@Autowired 
	private DayDao dayDao;
	@Autowired
	private HourDao hourDao;
	@Autowired
	private MinuteDao minuteDao;
	@Autowired
	private MonthDao monthDao;
	@Autowired
	private YearDao yearDao;
	
	@Override
	public List<ClientTicket> getTicket(int startCityId, int endCityId, int year, int month, int day) {
		List<Ticket> listTicket = ticketDao.getTicketes(startCityId, endCityId, year, month, day);
		List<ClientTicket> resultList = transformat(listTicket);
		return resultList;
	}

	private List<ClientTicket> transformat(List listTicket) {
		List<ClientTicket> resultList = new ArrayList<ClientTicket>();
		for(int i = 0; i < listTicket.size(); i++){
			ClientTicket clientTicket = new ClientTicket();
			Object[] objs = (Object[])listTicket.get(i);
			
			clientTicket.setBusType((String)objs[0]);
			clientTicket.setEndSite((String)objs[1]);
			clientTicket.setInventory((int)objs[2]);
			clientTicket.setPrice((float)objs[3]);
			clientTicket.setStartCity((String)objs[4]);
			clientTicket.setStartPassengerStation((String)objs[5]);
			clientTicket.setHour((int)objs[6]);
			clientTicket.setMinute((int)objs[7]);
			clientTicket.setId((int)objs[8]);
			clientTicket.setBusNumber((String)objs[9]);
			clientTicket.setMonth((int)objs[10]);
			clientTicket.setDay((int)objs[11]);
			
			clientTicket.setStartTime(clientTicket.getMonth() + "-" +
										clientTicket.getDay() + " " +
										clientTicket.getHour() + ":" + 
										clientTicket.getMinute());
			
			resultList.add(clientTicket);
		}
		Collections.sort(resultList);
		return resultList;
	}

	@Override
	public void addTicket(int startCityId, int startPassengerId, int endCityId, int endSiteId, String startTime,
			String busNumber, String busType, float price, int inventory) {
		String[] strTime = startTime.split("[- :]");
		
		Ticket ticket = new Ticket();
		ticket.setBusNumber(busNumber);
		ticket.setBusType(busType);
		ticket.setDay(dayDao.getOrAdd(Integer.parseInt(strTime[2])));//201605021904
		ticket.setEndCity(cityDao.get(endCityId));
		ticket.setEndSite(siteDao.get(endSiteId));
		ticket.setHour(hourDao.getOrAdd(Integer.parseInt(strTime[3])));
		ticket.setInventory(inventory);
		ticket.setMinute(minuteDao.getOrAdd(Integer.parseInt(strTime[4])));
		ticket.setMonth(monthDao.getOrAdd(Integer.parseInt(strTime[1])));
		ticket.setPrice(price);
		ticket.setStartCity(cityDao.get(startCityId));
		ticket.setStartPassengerStation(passengerStationDao.get(startPassengerId));
		ticket.setYear(yearDao.getOrAdd(Integer.parseInt(strTime[0])));
		
		SessionFactory sessionFactory = null;
		Session session = null;
		
		sessionFactory = ticketDao.getSessionFactory();
		session = sessionFactory.openSession();
		
		ticketDao.save(ticket);
		
		session.close();
	}

}
